package com.kakarote.bi.controller;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.kakarote.bi.entity.BO.BiExamineInfoBO;
import com.kakarote.bi.entity.VO.BiExamineStatisticsVO;
import com.kakarote.bi.entity.VO.BiWorkExamineCategoryVO;
import com.kakarote.bi.entity.VO.BiWorkLogStatisticsVO;
import com.kakarote.bi.service.BiWorkService;
import com.kakarote.core.common.Result;
import com.kakarote.core.entity.BasePage;
import com.kakarote.core.feign.crm.entity.BiEntityParams;
import com.kakarote.core.feign.oa.entity.ExamineVO;
import com.kakarote.core.feign.oa.entity.OaLogVO;
import com.kakarote.core.utils.ExcelParseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/biWork")
@Api(tags = "办公分析接口")
public class BiWorkController {

    @Autowired
    private BiWorkService biWorkService;


    @ApiOperation("查询日志统计信息")
    @PostMapping("/logStatistics")
    public Result<List<BiWorkLogStatisticsVO>> logStatistics(@RequestBody BiEntityParams biParams) {
        List<BiWorkLogStatisticsVO> objectList = biWorkService.logStatistics(biParams);
        return Result.ok(objectList);
    }


    @ApiOperation("查询日志统计信息导出")
    @PostMapping("/logStatisticsExport")
    public void logStatisticsExport(@RequestBody BiEntityParams biParams) throws IOException {
        List<BiWorkLogStatisticsVO> recordList = biWorkService.logStatistics(biParams);
        List<Map<String, Object>> collect = recordList.stream().map(BeanUtil::beanToMap).collect(Collectors.toList());
        List<ExcelParseUtil.ExcelDataEntity> dataList = new ArrayList<>();
        dataList.add(ExcelParseUtil.toEntity("realname", "员工"));
        dataList.add(ExcelParseUtil.toEntity("count", "填写数"));
        dataList.add(ExcelParseUtil.toEntity("unCommentCount", "未评论数"));
        dataList.add(ExcelParseUtil.toEntity("commentCount", "已评论数"));
        ExcelParseUtil.exportExcel(collect, new ExcelParseUtil.ExcelParseService() {
            @Override
            public String getExcelName() {
                return "日志统计";
            }

            @Override
            public boolean isXlsx() {
                return true;
            }
        }, dataList);
    }

    @ApiOperation("查询审批统计信息")
    @PostMapping("/examineStatistics")
    public Result<BiExamineStatisticsVO> examineStatistics(@RequestBody BiEntityParams biParams) {
        BiExamineStatisticsVO object = biWorkService.examineStatistics(biParams);
        return Result.ok(object);
    }


    @ApiOperation("查询审批统计信息导出")
    @PostMapping("/examineStatisticsExport")
    public void examineStatisticsExport(@RequestBody BiEntityParams biParams) throws IOException {
        BiExamineStatisticsVO object = biWorkService.examineStatistics(biParams);
        List<JSONObject> userList = object.getUserList();
        List<BiWorkExamineCategoryVO> categoryList = object.getCategoryList();
//        List<JSONObject> userList = object.getJSONArray("userList").toJavaList(JSONObject.class);
//        List<JSONObject> categoryList = object.getJSONArray("categoryList").toJavaList(JSONObject.class);
/*        userList.forEach(user -> {
            user.remove("img");
            user.remove("userId");
            user.remove("username");
        });*/
        List<ExcelParseUtil.ExcelDataEntity> dataList = new ArrayList<>();
        dataList.add(ExcelParseUtil.toEntity("realname", "员工"));
        for (BiWorkExamineCategoryVO record : categoryList) {
            dataList.add(ExcelParseUtil.toEntity("count_" + record.getCategoryId(), record.getTitle()));
        }
        ExcelParseUtil.exportExcel(userList, new ExcelParseUtil.ExcelParseService() {
            @Override
            public String getExcelName() {
                return "审批分析";
            }

            @Override
            public boolean isXlsx() {
                return true;
            }
        }, dataList);
    }


    /**
     * 查询审批详情
     *
     * @author zhangzhiwei
     */
    @ApiOperation("查询审批详情")
    @PostMapping("/examineInfo")
    public Result<BasePage<ExamineVO>> examineInfo(@RequestBody BiExamineInfoBO biEntityParams) {
        return Result.ok(biWorkService.examineInfo(biEntityParams));
    }

    /**
     * 查询评论日志列表
     */
    @ApiOperation("查询评论日志列表")
    @PostMapping("/queryOaComment")
    public Result<BasePage<OaLogVO>> queryOaComment(@RequestBody BiExamineInfoBO biEntityParams) {
        return Result.ok(biWorkService.queryOaComment(biEntityParams));
    }

    /**
     * 查询评论日志列表
     */
    @ApiOperation("查询评论日志列表")
    @PostMapping("/queryOaNoComment")
    public Result<BasePage<OaLogVO>> queryOaNoComment(@RequestBody BiExamineInfoBO biEntityParams) {
        return Result.ok(biWorkService.queryOaNoComment(biEntityParams));
    }
}
